package com.develop.web.in.service;

import com.develop.data.loan.model.LoanContract;
import com.develop.data.loan.model.LoanInstallment;
import com.develop.web.in.service.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class LoanContractHubService implements LoanContractCreateService<LoanContract, LoanContract> {

    @Autowired
    LoanRepaymentPlanHubService loanRepaymentPlanHubService;

    @Autowired
    @Qualifier("LoanContractJpaService")
    LoanContractCreateService<LoanContract, LoanContract> loanContractCreateService;

    @Autowired
    @Qualifier("LoanContractJpaService")
    LoanContractDeleteService<LoanContract, LoanContract> loanContractDeleteService;

    @Autowired
    @Qualifier("LoanContractJpaService")
    LoanContractUpdateService<LoanContract, LoanContract> loanContractUpdateService;

    @Autowired
    @Qualifier("LoanContractJpaService")
    LoanContractDetailService<LoanContract, LoanContract> loanContractDetailService;

    @Override
    public LoanContract batchCreate(LoanContract item) {
        List<LoanInstallment> installments = loanRepaymentPlanHubService.preview(item);
        item.setLoanInstallments(installments);
        return loanContractCreateService.batchCreate(item);
    };

    public List<LoanContract> batchCreate(List<LoanContract> items) {
        return items.stream().map(this::batchCreate).collect(Collectors.toList());
    };

}
